package com.quarters.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quarters.bean.Inquiry;
import com.quarters.bean.Quarters;
import com.quarters.bean.User;
import com.quarters.bean.layuiBean.ResultPage;
import com.quarters.bean.layuiBean.VoPage;
import com.quarters.bean.request.NewOrderTemplatData;
import com.quarters.mapper.InquiryMapper;
import com.quarters.service.InquiryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quarters.service.XbxWxService;
import com.quarters.util.exception.CommonException;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 报修查询表 服务实现类
 * </p>
 *
 * @author 冯政尚
 * @since 2019-09-02
 */
@Service
public class InquiryServiceImpl extends ServiceImpl<InquiryMapper, Inquiry> implements InquiryService {

    @Autowired
    XbxWxService xbxWxService;

    /**
     * @param voPage
     * @return 后台查询
     */
    @Override
    public ResultPage<Inquiry> selectMyPage(VoPage voPage,Long userId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1) * voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        paramMap.put("userId", userId);
        List<Inquiry> list = baseMapper.selectMyPage(paramMap);
        Integer count = baseMapper.countMyPage(paramMap);
        ResultPage<Inquiry> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }
    @Override
    public ResultPage<Inquiry> selectMyPageAll(VoPage voPage ) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("page", (voPage.getPage() - 1) * voPage.getLimit());
        paramMap.put("limit", voPage.getLimit());
        List<Inquiry> list = baseMapper.selectMyPageAll(paramMap);
        Integer count = baseMapper.countMyPageAll(paramMap);
        ResultPage<Inquiry> resultPage = new ResultPage<>();
        resultPage.setCode(0);
        resultPage.setCount(count);
        resultPage.setMsg("成功");
        resultPage.setData(list);
        return resultPage;
    }

    /**
     * @param quartersName 报修类型
     * 发送模板消息
     */
    @Override
    public void paySuccess(String quartersName,Long userId,Long createTime,String content,String phone) {
        //查询该用户所在小区的标签管理员是谁
        //根据当前用户查询所在的小区,在根据小区查询这个小区的给管理员是谁
        User user = baseMapper.selectOpenId(userId);
        System.out.println("user111111:"+user);
        if(user==null){
            throw new CommonException("该小区暂无管理员!");
        }
        //给所在小区的管理员发送模板消息
        NewOrderTemplatData newOrderTemplatData = new NewOrderTemplatData();
        //报修类型
        newOrderTemplatData.setType(quartersName);
        //发送给谁
        newOrderTemplatData.setToUser(user.getOpenid());
        //报修内容
        newOrderTemplatData.setContent(content);
        //创建时间
        newOrderTemplatData.setCreateTime(stampToDate(createTime + ""));
        //该标签管理员所在的小区
        newOrderTemplatData.setXiaoqu(user.getQuartersName());
        //手机号
        newOrderTemplatData.setPhone(phone);
        try {
            xbxWxService.questions(newOrderTemplatData);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
    }


    /*
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }

}
